package com.qf.MyFilter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qf.pojo.BaseResp;
import com.qf.utils.CookieUtils;
import com.qf.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class MyZuulFilter extends ZuulFilter {
    @Autowired
    RedisUtils redisUtils;
    @Override
    public String filterType() {
        return  FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();
        if (requestURI.equals("/bcy-user/user/login")||requestURI.equals("/bcy-user/user/regist")
                ||requestURI.equals("/bcy-picshare/picShare/findPic")||requestURI.equals("/bcy-picshare/picShare/findShare")){
            return false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //进行用户的认证
        //获取到request请求
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        //调用CookieUtils 获取到设置在 前端cookie中的uuid
        CookieUtils cookieUtils = new CookieUtils();
        String token = cookieUtils.getToken(request);
        BaseResp baseResp = new BaseResp();
        //获取到response
        HttpServletResponse response = currentContext.getResponse();
        //判断前端的cookie中如果为null则用户未登录
        if(token== null|| token.isEmpty()){
            baseResp.setCode(206);
            baseResp.setMessage("用户未登录");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            PrintWriter writer = null;
            try {
                writer = response.getWriter();
                writer.write(JSONObject.toJSONString(baseResp));
                //停止继续请求执行

            } catch (IOException e) {
                e.printStackTrace();
            }
            currentContext.setSendZuulResponse(false);
        }else {
            //如果前端cookie中含有值 则需要通过redis进行token的验证
            boolean b = redisUtils.hasKey(token);
            if (!b){
                //制定返回值
                baseResp.setCode(20007);
                baseResp.setMessage("用户token值无效");
                //对response进行操作
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json");
                PrintWriter writer = null;
                try {
                    writer = response.getWriter();
                    writer.write(JSONObject.toJSONString(baseResp));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                currentContext.setSendZuulResponse(false);
            }
        }
        //说明全部认证通过，放行处理
        return null;
    }
}
